package com.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import java.io.IOException;
import java.util.List;

/**
 * Created by hdu_hyg on 2017/12/27.
 */
public class JsonUtil {

    //jackson2.10 后 JsonMapper取代ObjectMapper
    private static final ObjectMapper objectMapper;
    static {
        objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule()); //处理 java8 time api

        //取消时间的转化格式,默认是时间戳,可以取消
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        //反序列化的时候如果多了其他属性,不抛出异常
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        //如果是空对象的时候,不抛异常
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    }

    public static String toJson(Object object){

        if (object == null) {
            return null;
        }

        String json = null;

        try {
            json = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    public static<T> T fromJson(String json, Class<T> classType){

        if (json == null) {
            return null;
        }

        T object = null;

        try {
            object = objectMapper.readValue(json, classType);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return object;
    }

    public static<T> List<T> getListFromJson(String json, Class<T> classType){

        if (json == null) {
            return null;
        }

        List<T> lists = null;

        try {
            JavaType javaType = objectMapper.getTypeFactory().constructCollectionType(List.class, classType);//ArrayList.class
            lists = objectMapper.readValue(json, javaType);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return lists;
    }

}
